package com.topscomm.mis.cbb.file.compare.word.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Objects;
import org.springframework.web.multipart.MultipartFile;

public class CbbFileUtil {

    /**
     * description multipartFile转File
     * @author wxm
     * @date 2023-06-17 08:18:50
     * @param file
     * @return java.io.File
     * @modify
     */
    public static File multipartFileToFile(MultipartFile file) throws Exception {
        File toFile = null;
        if (file == null || file.getSize() <= 0) {
            file = null;
        } else {
            InputStream ins = null;
            ins = file.getInputStream();
            toFile = new File(Objects.requireNonNull(file.getOriginalFilename()));
            inputStreamToFile(ins, toFile);
            ins.close();
        }
        return toFile;
    }

    /**
     * description 文件流转File
     * @author wxm
     * @date 2023-06-17 08:19:06
     * @param ins
     * @param file
     * @return void
     * @modify
     */
    public static void inputStreamToFile(InputStream ins, File file) {
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * description 附件保存至本地 - 测试用
     *
     * @param file
     * @return void
     * @author wxm
     * @date 2023-06-12 18:37:02
     * @modify
     */
    public static void downloadFile(File file) {

        InputStream in = null;
        OutputStream os = null;
        try {
            in = new FileInputStream(file);
            os = new FileOutputStream("D:/aa/123.zip");
            byte[] data = new byte[4096];
            int length = -1;
            while ((length = in.read(data)) != -1) {
                os.write(data, 0, length);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                try {
                    os.flush();
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * description 字节数组转File
     * @author wxm
     * @date 2023-06-17 10:26:52
     * @param bytes
     * @param filePath
     * @param fileName
     * @return void
     * @modify
     */
    public static void bytesToFile(byte[] bytes, String filePath, String fileName) throws IOException {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        File dir = new File(filePath);
        if (!dir.exists()) {// 判断文件目录是否存在
            dir.mkdirs();
        }
        file = new File(filePath + "//" + fileName);
        fos = new FileOutputStream(file);
        bos = new BufferedOutputStream(fos);
        bos.write(bytes);
        bos.close();
        fos.close();
    }
}